package com.kingsoft.miot.bridge.iam;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.kingsoft.miot.bridge.iam.common.AwsSignerV4Util;

public class TestGet {

	public static void main(String[] args) throws Exception {
		
		String api = "http://iam.api.ksyun.com/";
		StringBuffer response = new StringBuffer();
		String service = "iam";
		String region = "cn-beijing-6";

		String ak = "AKLTuJudK0klRmiH6gNWiXkFlA";
		String sk = "OBcrjHtdjbNsBB++Rl3MkdgoNQ/26jpGqkGe91PbgZeTNk7Rwbk7J1u0VGoBH5DDDg==";

//		Map<String, String> parameters = new HashMap<String, String>();
//		parameters.put("Action", "SetReferProtectionConfig");
//		parameters.put("Version", "2016-09-01");
//		parameters.put("DomainId", "2D09A6P");
//		parameters.put("Enable", "on");
//		parameters.put("ReferType", "allow");
//		parameters.put("ReferList",
//				"164.com6,164.com7,164.com8,164.com9,164.com10,164.com11,164.com12,164.com13,164.com14,164.com15,164.com16,164.com17,164.com18,164.com19,164.com20,164.com21,164.com22,164.com23,164.com24,164.com25,164.com26,164.com27,164.com28,164.com29,164.com30,164.com31,164.com32,164.com33,164.com34,164.com35,164.com36,164.com37,164.com38,164.com39,164.com40,164.com41,164.com42,164.com43,164.com44,164.com45,164.com46,164.com47,164.com48,164.com49,164.com50,164.com51,164.com52,164.com53,164.com54,164.com55,164.com56,164.com57,164.com58,164.com59,164.com60,164.com61,164.com62,164.com63,164.com64,164.com65,164.com66,164.com67,164.com68,164.com69,164.com70,164.com71,164.com72,164.com73,164.com74,164.com75,164.com76,164.com77,164.com78,164.com79,164.com80,164.com81,164.com82,164.com83,164.com84,164.com85,164.com86,164.com87,164.com88,164.com89,164.com90,164.com91,164.com92,164.com93,164.com94,164.com95,164.com96,164.com97,164.com98,164.com99,164.com100,164.com101,164.com102,164.com103,164.com104");

		Map<String, String> parameters = new HashMap<String, String>();

		parameters.put("Action", "CreateUser");
		parameters.put("Version", "2015-11-01");
		parameters.put("Path", "/");
		parameters.put("UserName", "hujianqing");
		parameters.put("RealName", "hujianqing");
		parameters.put("Phone", "13423456789");
		parameters.put("Email", "hujianqing@kingsoft.com");
		parameters.put("Remark", "createuser");
		parameters.put("AccountId", "2000090910");

		try {

			URI uri = new URI(api);
			String query = "?";
			for (Map.Entry<String, String> entry : parameters.entrySet()) {
				query = query + entry.getKey() + '=' + entry.getValue() + '&';
			}
			query = query.substring(0, query.length() - 1);
			Map<String, String> h = AwsSignerV4Util.getAuthHeaderForGet(uri, parameters, null, service, region, ak, sk);

			System.out.println(query);
			URL url = new URL(api + query);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//			conn.setConnectTimeout(10000);
			conn.setDoOutput(true);

			// By default it is GET request
			conn.setRequestMethod("GET");

			// add request header
//			conn.setRequestProperty("Accept", "application/json");

			for (Map.Entry<String, String> entry : h.entrySet()) {
				System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
				conn.setRequestProperty(entry.getKey(), entry.getValue());
			}

			int responseCode = conn.getResponseCode();

			System.out.println("Sending get request : " + url);
			System.out.println("Response code : " + responseCode);

			// Reading response from input Stream
			BufferedReader in;
			if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
				in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			} else {
				in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
			}
			String output;

			while ((output = in.readLine()) != null) {
				response.append(output);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
//			System.out.println(e.toString());
		}

		// printing result from response
		System.out.println(response.toString());

	}
}
